package com.blockchain.securewallet.model.db.oper;

import android.content.Context;

import com.blockchain.securewallet.model.db.UnSpent;
import com.blockchain.securewallet.model.db.dao.AccountDao;
import com.blockchain.securewallet.model.db.dao.UnSpentDao;
import com.blockchain.securewallet.model.db.manager.DbManager;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * Created by Chain-Cloud on 2018/6/12.
 */

public class UnSpentDaoOpe {

    /**
     * 添加数据至数据库
     *
     * @param context
     * @param stu
     */
    public static void insertData(Context context, UnSpent stu) {
        DbManager.getDaoSession(context).getUnSpentDao().insert(stu);
    }


    /**
     * 将数据实体通过事务添加至数据库
     *
     * @param context
     * @param list
     */
    public static void insertData(Context context, List<UnSpent> list) {
        if (null == list || list.size() <= 0) {
            return;
        }
        DbManager.getDaoSession(context).getUnSpentDao().insertInTx(list);
    }

    /**
     * 添加数据至数据库，如果存在，将原来的数据覆盖
     * 内部代码判断了如果存在就update(entity);不存在就insert(entity)；
     *
     * @param context
     * @param student
     */
    public static void saveData(Context context, UnSpent student) {
        DbManager.getDaoSession(context).getUnSpentDao().save(student);
    }

    /**
     * 删除数据至数据库
     *
     * @param context
     * @param student 删除具体内容
     */
    public static void deleteData(Context context, UnSpent student) {
        DbManager.getDaoSession(context).getUnSpentDao().delete(student);
    }

//    /**
//     * 根据id删除数据至数据库
//     *
//     * @param context
//     * @param id      删除具体内容
//     */
//    public static void deleteByKeyData(Context context, long id) {
//        DbManager.getDaoSession(context).getUnSpentDao().deleteByKey(id);
//    }

    /**
     * 删除全部数据
     *
     * @param context
     */
    public static void deleteAllData(Context context) {
        DbManager.getDaoSession(context).getUnSpentDao().deleteAll();
    }

    /**
     * 更新数据库
     *
     * @param context
     * @param student
     */
    public static void updateData(Context context, UnSpent student) {
        DbManager.getDaoSession(context).getUnSpentDao().update(student);
    }


    /**
     * 查询所有数据
     *
     * @param context
     * @return
     */
    public static List<UnSpent> queryAll(Context context) {
        QueryBuilder<UnSpent> builder = DbManager.getDaoSession(context).getUnSpentDao().queryBuilder().orderDesc(AccountDao.Properties.Time);

        return builder.build().list();
    }


    public static void deleteDataFromAddress(Context context,String address) {
        List<UnSpent> unSpentList=DbManager.getDaoSession(context).getUnSpentDao().queryBuilder().where(UnSpentDao.Properties.Address.eq(address)).build().list();
        DbManager.getDaoSession(context).getUnSpentDao().deleteInTx(unSpentList);
    }

    public static List<UnSpent> queryDateFromAddress(Context context,String address) {
        List<UnSpent> unSpentList=DbManager.getDaoSession(context).getUnSpentDao().queryBuilder().where(UnSpentDao.Properties.Address.eq(address)).build().list();
        return unSpentList;
    }
}
